package Month11.Day23;

public class T2 {
    /*
    baidu 一个线程死锁的例子:

   */
    public static void main(String[] args) {
        //内个贷源
        final Object resource1 = "resource1";
        final Object resource2 = "resource2";
//第一个线程， 想先占有resource1,再尝试占有resource2
        Thread t1 = new Thread() {
            public void run() {
//尝试占有resource1
                synchronized (resource1) {
//成功占有resource1
                    System.out.println("Thread1: locked resource 1");
//休眠-段时间
                    try {
                        Thread.sleep(50);
                    } catch (Exception e) {
//尝试占有resource2，如果不能占有，该线程将会一-直等待
                        synchronized (resource2) {
                            synchronized (resource2) {
                                System.out.println("Thread1 : locked resource");
                            }
                        }
                    }
                }

            }
        };
        //第二个线程，想先占有resource2.再占有resourvce1
        Thread t2 = new Thread() {
            public void run() {
//尝试占有resource2
                synchronized (resource2) {
//成功占有r2
                    System.out.println("Thread2:locked resource2");
//休眠一段时间
                    try {
                        Thread.sleep(50);
                    } catch (Exception e) {
// TODO: handLe exception
//尝试占有resource1,如果不能占有，该该线程将会- -直等待
                        synchronized (resource1) {
                            System.out.println("Thread2 : locked resource1");
                        }
                    }
                }

//启动两个线程
                t1.start();
//                t2.start();


            }
        };
    }
}